﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text.RegularExpressions;
namespace syc.Sale
{
    public partial class GuanCai_DaoHuo_Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String sql = @"select t1.ID
,t1.原料ID
,t1.供货ID
,case when t1.分厂=0 then '待定'
when t1.分厂=1 then '一分厂'
when t1.分厂=2 then '二分厂'
when t1.分厂=3 then '三分厂'
when t1.分厂=4 then '四分厂'
when t1.分厂=5 then '五分厂'
else '未知分厂'
end as 使用单位
,t1.库区
,t1.日期
,t1.磅重1 
,t1.磅重2
,t1.磅重
,t1.标重
,t1.磅重-t1.标重 as 磅差
,t1.支数
,case when t1.状态=0 then '未送检'
when t1.状态=1 then '待检'
when t1.状态=2 then '合格'
when t1.状态=3 then '已发货'
else '未知状态'end as 状态描述
,case when t1.原料ID=1 then '油管光管(J55)'
when t1.原料ID=2 then '套管光管(J55)'
when t1.原料ID=3 then '油管接箍光管(J55)'
when t1.原料ID=4 then '套管接箍光管(J55)'
when t1.原料ID=5 then '成品油管接箍(J55)'
when t1.原料ID=6 then '成品套管接箍(J55)'
when t1.原料ID=101 then '油管光管(N80Q)'
when t1.原料ID=102 then '套管光管(N80Q)'
when t1.原料ID=103 then '油管接箍光管(N80Q)'
when t1.原料ID=104 then '套管接箍光管(N80Q)'
when t1.原料ID=105 then '成品油管接箍(N80Q)'
when t1.原料ID=106 then '成品套管接箍(N80Q)'
else '其他原料' end as 原料
,t2.名称 as 供货单位
,t1.车号
,t1.是否暂估入库
from 原料到货记录 t1
left outer join 原料供货单位 t2 on t2.ID=t1.供货ID";
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                this.SqlDataSource1.SelectCommand = sql + " order by 日期 desc";
                this.ViewState["SelectCommand"] = sql + " order by 日期 desc";
            }
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            //string temp = this.CheHao.Text.Trim();
            //string temp1 = this.LuHao.Text.Trim();
            string Sql = " where 0=0";         
            //if (temp == "" && temp1 == "")
            //{
            //    Sql = "";
            //}
            //else if (temp1 == "")
            //{
            //    Sql = " where  dbo.原料到发记录.[车号] like '%" + temp + "%'";
            //}
            //else if (temp == "")
            //{
            //    Sql = " where  原料到发明细.[炉号] like '%" + temp1 + "%'";
            //}
            //else
            //{
            //    Sql = " where  原料到发记录.[车号] like '%" + temp + "%' and dbo.原料到发明细.[炉号] like '%" + temp1 + "%'";
            //}
           
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                Sql += " and t1.[日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                Sql += " and t1.[日期]<='" + Dt.ToShortDateString() + "'";
            }
            if (this.GongHuoDanWei.Text != "0")
            {
                Sql += " and t1.供货ID=" + this.GongHuoDanWei.Text;
            }
            if (this.YuanCaiLiao.Text != "0")
            {
                Sql += " and t1.原料ID=" + this.YuanCaiLiao.Text;
            }
            if (this.CheHao.Text.Trim() != "")
            {
                Sql += " and t1.车号='" + this.CheHao.Text.Trim()+"'";
            }
            if (this.Ruku.Text != "-1")
            {
                Sql += " and t1.是否暂估入库=" + this.Ruku.Text;
            }
            string sql = @"select t1.ID
,t1.原料ID
,t1.供货ID
,case when t1.分厂=0 then '待定'
when t1.分厂=1 then '一分厂'
when t1.分厂=2 then '二分厂'
when t1.分厂=3 then '三分厂'
when t1.分厂=4 then '四分厂'
when t1.分厂=5 then '五分厂'
else '未知分厂'
end as 使用单位
,t1.库区
,t1.日期
,t1.磅重1 
,t1.磅重2
,t1.磅重
,t1.标重
,t1.磅重-t1.标重 as 磅差
,t1.支数
,case when t1.状态=0 then '未送检'
when t1.状态=1 then '待检'
when t1.状态=2 then '合格'
when t1.状态=3 then '已发货'
else '未知状态'end as 状态描述
,case when t1.原料ID=1 then '油管光管(J55)'
when t1.原料ID=2 then '套管光管(J55)'
when t1.原料ID=3 then '油管接箍光管(J55)'
when t1.原料ID=4 then '套管接箍光管(J55)'
when t1.原料ID=5 then '成品油管接箍(J55)'
when t1.原料ID=6 then '成品套管接箍(J55)'
when t1.原料ID=101 then '油管光管(N80)'
when t1.原料ID=102 then '套管光管(N80)'
when t1.原料ID=103 then '油管接箍光管(N80)'
when t1.原料ID=104 then '套管接箍光管(N80)'
when t1.原料ID=105 then '成品油管接箍(N80)'
when t1.原料ID=106 then '成品套管接箍(N80)'
else '其他原料' end as 原料
,t2.名称 as 供货单位
,t1.车号
,t1.是否暂估入库
from 原料到货记录 t1
left outer join 原料供货单位 t2 on t2.ID=t1.供货ID
@where
 ";
           sql = sql.Replace("@where", Sql);
           this.SqlDataSource1.SelectCommand = sql+"order by 日期 desc";
//            string sql_temp = @"SELECT 车数=(select count(distinct dbo.管材到货登记.车号) from dbo.管材到货登记 INNER JOIN
//                      dbo.管材到货明细 ON dbo.管材到货登记.ID = dbo.管材到货明细.管材到货登记ID INNER JOIN
//                      dbo.原料供货单位 ON dbo.管材到货登记.原料供货单位ID = dbo.原料供货单位.ID INNER JOIN
//                      dbo.原料 ON dbo.管材到货明细.管材ID=dbo.原料.ID  " + Sql + "), isnull(SUM(管材到货明细.重量),0),isnull(SUM(磅重),0) FROM  dbo.管材到货登记 INNER JOIN dbo.管材到货明细 ON dbo.管材到货登记.ID = dbo.管材到货明细.管材到货登记ID INNER JOIN dbo.原料供货单位 ON dbo.管材到货登记.原料供货单位ID = dbo.原料供货单位.ID INNER JOIN  dbo.原料 ON dbo.管材到货明细.管材ID = dbo.原料.ID " + Sql;
           
//           SqlConnection Conn = new SqlConnection();
//           Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
//           Conn.Open();
//           SqlCommand Cmd = new SqlCommand();
//           Cmd.Connection = Conn;        
//           int 车数 = 0;
//           double 名义重量 = 0;
//           double 实际磅重 = 0;
//           double 磅差 = 0;
//           int 支数 = 0;
////            string 记录="";
////            string[] 记录ID;
////            this.Label_Result.Text = "";          
////            string sql1 = @"select distinct 原料到发记录.ID  from 原料到发明细 inner join 原料到发记录 on 原料到发明细.记录ID=原料到发记录.ID
////inner join 原料 ON dbo.原料到发明细.原料ID =原料.ID
////inner join 原料供货单位 on 原料到发明细.供货ID=原料供货单位.ID  @条件
////           ";
////            sql1 = sql1.Replace("@条件", Sql);
////            Cmd.CommandText = sql1;
////            SqlDataReader Reader = Cmd.ExecuteReader();            
////            while (Reader.Read())
////            {
////                记录+=((int)Reader[0]).ToString()+"a";                
////            }
////            记录ID = Regex.Split(记录, "a", RegexOptions.IgnoreCase);
////            Reader.Close();
////            string sql2 = @"
////            declare @名义重量 float
////            declare @实际重量 float
////            declare @磅差 float
////            declare @支数 int
////            select @名义重量=isnull(sum(总重量),0),@实际重量=isnull(sum(磅重),0),@磅差=isnull((sum(磅重)-sum(总重量)),0),@支数=isnull(sum(总支数),0) from 原料到发记录 where ID between @parm1 and @parm2
////            select @名义重量,@实际重量,@磅差,@支数";
////            sql2 = sql2.Replace("@parm1", 记录ID[0]);
////            sql2 = sql2.Replace("@parm2", 记录ID[记录ID.Length-1-1]);
////            Cmd.CommandText = sql2;
////            Reader = Cmd.ExecuteReader();
////            while (Reader.Read())
////            {
////                名义重量 = (double)Reader[0];
////                实际磅重= (double)Reader[1];
////                磅差 = (double)Reader[2];
////                支数= (int)Reader[3];
////            }
////            Reader.Close();
////            DataSet ds = new DataSet();
////            string sql = "select distinct 车号 from 原料到发记录 inner join 原料到发明细 on 原料到发记录.ID=原料到发明细.记录ID" + Sql;
////            SqlDataAdapter sda = new SqlDataAdapter(sql, Conn);
////            sda.Fill(ds);
////            车数 = ds.Tables[0].Rows.Count;
//           string sql1 = @"
//            declare @名义重量 float
//            declare @实际重量 float
//            declare @磅差 float
//            declare @支数 int
//select 
// @实际重量=isnull(sum(t1.磅重),0)
//,@名义重量=isnull(sum(t1.总重量),0)
//,@支数=isnull(sum(t1.总支数),0)
//from 原料到发记录 t1
//left outer join 原料供货单位 t2 on t2.ID=t1.供货ID
//where t1.ID in  (select 原料到发明细.记录ID from 原料到发明细 inner join 原料到发记录 on 原料到发明细.记录ID=原料到发记录.ID
//inner join 原料 ON dbo.原料到发明细.原料ID =原料.ID
//inner join 原料供货单位 on 原料到发明细.供货ID=原料供货单位.ID
//@where
//) 
//select @实际重量,@名义重量,@支数
//";
//           sql1 = sql1.Replace("@where", Sql);
//           Cmd.CommandText = sql1;

//           SqlDataReader Reader = Cmd.ExecuteReader();
//           if (Reader.Read())
//           {               
//               实际磅重 = (double)Reader[0];
//               名义重量 = (double)Reader[1];
//               磅差 = 实际磅重 - 名义重量;
//               支数 = (int)Reader[2];
//           }
//           Reader.Close();
//           DataSet ds = new DataSet();
//           string sql2 = "select distinct 车号 from 原料到发记录 inner join 原料到发明细 on 原料到发记录.ID=原料到发明细.记录ID" + Sql;
//           //Response.Write(sql2);
//           //Response.End();
//           SqlDataAdapter sda = new SqlDataAdapter(sql2, Conn);
//           sda.Fill(ds);
//           车数 = ds.Tables[0].Rows.Count;
//           this.CS.Text =  "车数："+车数.ToString()+" 车";
//           Reader.Dispose();
//           Cmd.Dispose();
//           Conn.Close();
//           Conn.Dispose();
            this.ViewState["SelectCommand"] = this.SqlDataSource1.SelectCommand;
        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }
        }

        protected void Button_Clear_Click(object sender, EventArgs e)
        {
            this.GongHuoDanWei.SelectedIndex = 0;
            this.YuanCaiLiao.SelectedIndex = 0;
            ////this.LuHao.Text = "";
            ////this.CheHao.Text = "";
            this.Date1.Text = "";
            this.Date2.Text = "";
           
        }

        protected void PageSizeOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageSize = int.Parse(this.PageSizeOption.Text);
        }

        private string GetSelectedList()
        {
            string s = "";
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                if (((CheckBox)this.GridView1.Rows[i].FindControl("Check_Item")).Checked)
                {                    
                    s += "," + this.GridView1.DataKeys[i].Values[0].ToString();
                }
            }
            if (s.Length > 0)
            {
                s = s.Substring(1);
            }
            return s;
        }

        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            string list = GetSelectedList();
            if (list == "")
            {
                this.Message.Text = "请点击表格前列的选择框，勾选要操作的记录。";
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/原料到货记录录入.aspx?list=" + list + "',740,600,'yes');</script>");
            }
        }

        protected void Check_All_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox s = (CheckBox)sender;
            string cs = "grid-row-normal";
            if (s.Checked)
            {
                cs = "grid-row-selected";
            }
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                ((CheckBox)this.GridView1.Rows[i].FindControl("Check_Item")).Checked = s.Checked;
                this.GridView1.Rows[i].CssClass = cs;
            }
           // this.Button_Delete.Enabled = false;
          //  this.Set_Button_State();
        }
        protected void Check_Item_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox s = (CheckBox)sender;
            string cs = "grid-row-normal";
            if (s.Checked)
            {
                cs = "grid-row-selected";
            }

            ((GridViewRow)s.Parent.Parent).CssClass = cs;
            string list = GetSelectedList();
            //if (list != "" && list.IndexOf(',') < 0)
            //{
            //    this.Button_Delete.Enabled = true;
            //}
            //else
            //{
            //    this.Button_Delete.Enabled = false;
            //}
            //this.Set_Button_State();
        }

        protected void Button_To_Click(object sender, EventArgs e)
        {
            string part1 = @"<html xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:x='urn:schemas-microsoft-com:office:excel'
xmlns='http://www.w3.org/TR/REC-html40'>
<meta http-equiv=Content-Type content='text/html; charset=utf-8'>
<!--[if gte mso 9]><xml>
 <o:DocumentProperties>
  <o:Author>Chinese User</o:Author>
  <o:LastAuthor>Chinese User</o:LastAuthor>
  <o:LastPrinted>2010-07-08T09:01:11Z</o:LastPrinted>
  <o:Created>2010-07-08T02:03:52Z</o:Created>
  <o:LastSaved>2010-07-08T09:01:30Z</o:LastSaved>
  <o:Company>Chinese ORG</o:Company>
  <o:Version>11.9999</o:Version>
 </o:DocumentProperties>
</xml><![endif]-->
<style>
<!--table
	{mso-displayed-decimal-separator:'\.';
	mso-displayed-thousand-separator:'\,';}
@page
	{margin:.7in .75in 1.0in .74in;
	mso-header-margin:.5in;
	mso-footer-margin:.5in;}
tr
	{mso-height-source:auto;
	mso-ruby-visibility:none;}
col
	{mso-width-source:auto;
	mso-ruby-visibility:none;}
br
	{mso-data-placement:same-cell;}
.style0
	{mso-number-format:General;
	text-align:general;
	vertical-align:middle;
	white-space:nowrap;
	mso-rotate:0;
	mso-background-source:auto;
	mso-pattern:auto;
	color:windowtext;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	font-family:宋体;
	mso-generic-font-family:auto;
	mso-font-charset:134;
	border:none;
	mso-protection:locked visible;
	mso-style-name:常规;
	mso-style-id:0;}
td
	{mso-style-parent:style0;
	padding-top:1px;
	padding-right:1px;
	padding-left:1px;
	mso-ignore:padding;
	color:windowtext;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	font-family:宋体;
	mso-generic-font-family:auto;
	mso-font-charset:134;
	mso-number-format:General;
	text-align:general;
	vertical-align:middle;
	border:none;
	mso-background-source:auto;
	mso-pattern:auto;
	mso-protection:locked visible;
	white-space:nowrap;
	mso-rotate:0;}
.xl24
	{mso-style-parent:style0;
	font-size:9.0pt;
	text-align:center;
	border-top:none;
	border-right:.5pt solid windowtext;
	border-bottom:.5pt solid windowtext;
	border-left:.5pt solid windowtext;}
.xl25
	{mso-style-parent:style0;
	font-size:9.0pt;
	text-align:center;
	border-top:none;
	border-right:.5pt solid windowtext;
	border-bottom:.5pt solid windowtext;
	border-left:none;}
.xl26
	{mso-style-parent:style0;
	color:black;
	font-size:9.0pt;
	text-align:center;
	border-top:none;
	border-right:.5pt solid windowtext;
	border-bottom:.5pt solid windowtext;
	border-left:none;}
.xl27
	{mso-style-parent:style0;
	font-size:9.0pt;
	text-align:center;
	border:.5pt solid windowtext;}
.xl28
	{mso-style-parent:style0;
	font-size:16.0pt;
	font-weight:700;
	text-align:center;
	border-top:none;
	border-right:none;
	border-bottom:.5pt solid windowtext;
	border-left:none;}
ruby
	{ruby-align:left;}
rt
	{color:windowtext;
	font-size:9.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	font-family:宋体;
	mso-generic-font-family:auto;
	mso-font-charset:134;
	mso-char-type:none;
	display:none;}
-->
</style>
<!--[if gte mso 9]><xml>
 <x:ExcelWorkbook>
  <x:ExcelWorksheets>
   <x:ExcelWorksheet>
    <x:Name>Sheet1</x:Name>
    <x:WorksheetOptions>
     <x:DefaultRowHeight>285</x:DefaultRowHeight>
     <x:Print>
      <x:ValidPrinterInfo/>
      <x:PaperSizeIndex>9</x:PaperSizeIndex>
      <x:HorizontalResolution>600</x:HorizontalResolution>
      <x:VerticalResolution>600</x:VerticalResolution>
     </x:Print>
     <x:Selected/>
     <x:Panes>
      <x:Pane>
       <x:Number>3</x:Number>
       <x:ActiveRow>10</x:ActiveRow>
       <x:ActiveCol>3</x:ActiveCol>
      </x:Pane>
     </x:Panes>
     <x:ProtectContents>False</x:ProtectContents>
     <x:ProtectObjects>False</x:ProtectObjects>
     <x:ProtectScenarios>False</x:ProtectScenarios>
    </x:WorksheetOptions>
   </x:ExcelWorksheet>
   <x:ExcelWorksheet>
    <x:Name>Sheet2</x:Name>
    <x:WorksheetOptions>
     <x:DefaultRowHeight>285</x:DefaultRowHeight>
     <x:ProtectContents>False</x:ProtectContents>
     <x:ProtectObjects>False</x:ProtectObjects>
     <x:ProtectScenarios>False</x:ProtectScenarios>
    </x:WorksheetOptions>
   </x:ExcelWorksheet>
   <x:ExcelWorksheet>
    <x:Name>Sheet3</x:Name>
    <x:WorksheetOptions>
     <x:DefaultRowHeight>285</x:DefaultRowHeight>
     <x:ProtectContents>False</x:ProtectContents>
     <x:ProtectObjects>False</x:ProtectObjects>
     <x:ProtectScenarios>False</x:ProtectScenarios>
    </x:WorksheetOptions>
   </x:ExcelWorksheet>
  </x:ExcelWorksheets>
  <x:WindowHeight>6750</x:WindowHeight>
  <x:WindowWidth>11700</x:WindowWidth>
  <x:WindowTopX>240</x:WindowTopX>
  <x:WindowTopY>105</x:WindowTopY>
  <x:ProtectStructure>False</x:ProtectStructure>
  <x:ProtectWindows>False</x:ProtectWindows>
 </x:ExcelWorkbook>
</xml><![endif]-->
</head>

<body link=blue vlink=purple>

<table x:str border=0 cellpadding=0 cellspacing=0 width=635 style='border-collapse:
 collapse;table-layout:fixed;width:477pt'>
 <col width=73 style='mso-width-source:userset;mso-width-alt:2336;width:55pt'>
 <col width=75 style='mso-width-source:userset;mso-width-alt:2400;width:56pt'>
 <col width=108 style='mso-width-source:userset;mso-width-alt:3456;width:81pt'>
 <col width=66 span=2 style='mso-width-source:userset;mso-width-alt:2112;
 width:50pt'>
 <col width=60 span=3 style='mso-width-source:userset;mso-width-alt:1920;
 width:45pt'>
 <col width=67 style='mso-width-source:userset;mso-width-alt:2144;width:50pt'>
 <tr height=39 style='mso-height-source:userset;height:29.25pt'>
  <td colspan=9 height=39 class=xl28 width=635 style='height:29.25pt;
  width:477pt'>到 货 结 果</td>
 </tr>
 <tr height=21 style='mso-height-source:userset;height:15.95pt'>
  <td height=21 class=xl24 style='height:15.95pt'>车号</td>
  <td class=xl25>供货单位</td>
  <td class=xl25>原料</td>
  <td class=xl26>磅重1(吨)</td>
  <td class=xl26>磅重2(吨)</td>
  <td class=xl26>磅重(吨)</td>
  <td class=xl26>标重(吨)</td>
  <td class=xl25>支数(支)</td>
  <td class=xl25>到货日期</td>
 </tr>
 ";
            string part3 = @"<![if supportMisalignedColumns]>
 <tr height=0 style='display:none'>
  <td width=77 style='width:58pt'></td>
  <td width=99 style='width:74pt'></td>
  <td width=70 style='width:53pt'></td>
  <td width=91 style='width:68pt'></td>
  <td width=66 style='width:50pt'></td>
  <td width=88 style='width:66pt'></td>
  <td width=70 style='width:53pt'></td>
  <td width=84 style='width:63pt'></td>
 </tr>
 <![endif]>
</table>

</body>

</html>";
            Response.Charset = "utf-8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=\"" + Server.UrlEncode("到货结果") + ".xls\"");
            Response.Write(part1);
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            string Sql = " where 0=0";
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                Sql += " and t1.[日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                Sql += " and t1.[日期]<='" + Dt.ToShortDateString() + "'";
            }
            if (this.GongHuoDanWei.Text != "0")
            {
                Sql += " and t1.供货ID=" + this.GongHuoDanWei.Text;
            }
            if (this.YuanCaiLiao.Text != "0")
            {
                Sql += " and t1.原料ID=" + this.YuanCaiLiao.Text;
            }
            if (this.CheHao.Text.Trim() != "")
            {
                Sql += " and t1.车号=" + this.CheHao.Text.Trim();
            }
       
            string sql1 = @"select t1.ID
,t1.原料ID
,t1.供货ID
,case when t1.分厂=0 then '待定'
when t1.分厂=1 then '一分厂'
when t1.分厂=2 then '二分厂'
when t1.分厂=3 then '三分厂'
when t1.分厂=4 then '四分厂'
when t1.分厂=5 then '五分厂'
else '未知分厂'
end as 使用单位
,t1.库区
,t1.日期
,t1.磅重1
,t1.磅重2
,t1.磅重
,t1.标重
,t1.磅重-t1.标重 as 磅差
,t1.支数
,case when t1.状态=0 then '未送检'
when t1.状态=1 then '待检'
when t1.状态=2 then '合格'
when t1.状态=3 then '已发货'
else '未知状态'end as 状态描述
,case when t1.原料ID=1 then '油管光管(J55)'
when t1.原料ID=2 then '套管光管(J55)'
when t1.原料ID=3 then '油管接箍光管(J55)'
when t1.原料ID=4 then '套管接箍光管(J55)'
when t1.原料ID=5 then '成品油管接箍(J55)'
when t1.原料ID=6 then '成品套管接箍(J55)'
when t1.原料ID=101 then '油管光管(N80)'
when t1.原料ID=102 then '套管光管(N80)'
when t1.原料ID=103 then '油管接箍光管(N80)'
when t1.原料ID=104 then '套管接箍光管(N80)'
when t1.原料ID=105 then '成品油管接箍(N80)'
when t1.原料ID=106 then '成品套管接箍(N80)'
else '其他原料' end as 原料
,t2.名称 as 供货单位
,t1.车号
from 原料到货记录 t1
left outer join 原料供货单位 t2 on t2.ID=t1.供货ID
@where
 ";
            sql1 = sql1.Replace("@where", Sql);
            Cmd.CommandText= sql1 + "order by 日期 desc";
            SqlDataReader Reader = Cmd.ExecuteReader();
            decimal  sum1 = 0, sum2 = 0, sum3 = 0;
            int sum4=0;
            while(Reader.Read())
            {
                Response.Write("<tr height=19 style='height:14.25pt'><td height=19 class=xl24 style='height:14.25pt;border-top:none'>"+Reader.GetString(15)+"</td>");
                Response.Write(" <td class=xl27>" + Reader.GetString(14) + "</td>");
                Response.Write(" <td class=xl27>" + Reader.GetString(13) + "</td>");
                Response.Write(" <td class=xl27 x:num='" + Reader.GetDecimal(6).ToString() + "'>" + ((String)Reader.GetDecimal(6).ToString()) + "</td>");
                Response.Write(" <td class=xl27 x:num='" + Reader.GetDecimal(7).ToString() + "'>" + ((String)Reader.GetDecimal(7).ToString()) + "</td>");
                Response.Write(" <td class=xl27 x:num='" + Reader.GetDecimal(8).ToString() + "'>" + ((String)Reader.GetDecimal(8).ToString()) + "</td>");
                Response.Write(" <td class=xl27 x:num='" + Reader.GetDecimal(9).ToString() + "'>" + ((String)Reader.GetDecimal(9).ToString()) + "</td>");
                Response.Write(" <td class=xl27 x:num='" + Reader.GetInt32(11).ToString() + "'>" + ((String)Reader.GetInt32(11).ToString()) + "</td>");
                Response.Write(" <td class=xl27>" + Reader.GetDateTime(5).ToString("yyyy-MM-dd") + "</td>");    
                sum1+=Reader.GetDecimal(7);
                sum2+=Reader.GetDecimal(8);
                sum3+=Reader.GetDecimal(9);
                sum4+=Reader.GetInt32(11);
            }
            Response.Write("<tr height=19 style='height:14.25pt'><td height=19 class=xl24 style='height:14.25pt;border-top:none'>备注</td>");
            Response.Write("<td class=xl27 colspan=8>实际总磅量：" + sum1 + " 吨 质保书总磅量：" + sum2 + " 吨 磅差：" + sum3 + " 吨 总支数：" + sum4 + " 支 </td>"); 
              Reader.Close();
              Conn.Close();
              Reader.Dispose();
              Cmd.Dispose();
              Conn.Dispose();
              Response.Write(part3);
              Response.End();
        }
        //是否暂估入库
        protected void CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            int k = 0;
            if (((CheckBox)sender).Checked) k = 1;
            if (k == 1)
            {
                this.SqlDataSource1.UpdateCommand = "update [原料到货记录] set [是否暂估入库]=" + k + " where id=" + ((CheckBox)sender).ToolTip;
            }
            else
            {
                this.SqlDataSource1.UpdateCommand = "update [原料到货记录] set [是否暂估入库]=" + k + " where id=" + ((CheckBox)sender).ToolTip;
            }
            this.SqlDataSource1.Update();
            this.GridView1.SelectedIndex = -1;
        }

    }
}
